import pandas as pd

import numpy as np
import scipy.sparse as ss
import scipy.io as sio

#保存数据
import pickle as cPickle

from sklearn.preprocessing import normalize

#读取训练集和测试集中出现过的事件列表
eventIndex = cPickle.load(open("PE_eventIndex.pkl", 'rb'))
n_events = len(eventIndex)

print("number of events in train & test :%d" % n_events)

# 读取数据
# """
#   统计某个活动，参加和不参加的人数，计算活动热度
# """

# 活动活跃度
eventPopularity = ss.dok_matrix((n_events, 1))

f = open("event_attendees.csv", 'r')

# 字段：event_id,yes, maybe, invited, and no
f.readline()  # skip header

for line in f:
    cols = line.strip().split(",")
    eventId = str(cols[0])  # event_id
    if eventId in eventIndex:
        i = eventIndex[eventId]  # 事件索引

        # yes - no
        eventPopularity[i, 0] = \
            len(cols[1].split(" ")) - len(cols[4].split(" "))

f.close()

eventPopularity = normalize(eventPopularity, norm="l1",
                            axis=0, copy=False)
sio.mmwrite("EA_eventPopularity", eventPopularity)

print(eventPopularity.todense())